import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class ThirdPartyApp extends StatefulWidget {
  ThirdPartyApp({Key key}) : super(key: key);

  @override
  _ThirdPartyAppState createState() => _ThirdPartyAppState();
}

class _ThirdPartyAppState extends State<ThirdPartyApp> {
  _launchURL() async {
    const url = 'https://flutter.dev';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _openMap() async {
    const url = "geo:52.32,4.917";
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _openYoutube() async {
    // Android
    const url = 'vnd.youtube://';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      //  Ios
      const url = 'youtube://';
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }
  }

  _call() async {
    const tel = 'tel:18618324939';
    if (await canLaunch(tel)) {
      await launch(tel);
    } else {
      throw 'Could not launch $tel';
    }
  }

  _sms() async {
    const sms = 'sms:18618324939';
    if (await canLaunch(sms)) {
      await launch(sms);
    } else {
      throw 'Could not launch $sms';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('如何打开第三方app'),
      ),
      body: ListView(
        children: [
          FlatButton(
            onPressed: _launchURL,
            child: Text('点击'),
          ),
          FlatButton(
            onPressed: _openMap,
            child: Text('打开地图'),
          ),
          FlatButton(
            onPressed: _openYoutube,
            child: Text('打开Youtube'),
          ),
          FlatButton(
            child: Text('发短信'),
            onPressed: _sms,
          ),
          FlatButton(
            child: Text('打电话'),
            onPressed: _call,
          )
        ],
      ),
    );
  }
}
